<?php

class models_PeriodMapper extends models_MapperBase
{
    public static $_dbTable = 'models_DbTable_Period';

//----------------------------------------------------------------------------------------------------

    protected static function _initItem($oRow)
    {
        if (null === $oRow)
        {
            return null;
        }

        $oItem = new models_Project();


        if (isset($oRow->id))
            $oItem->id          = $oRow->id;
        if (isset($oRow->id_user))
            $oItem->id_user     = $oRow->id_user;
        if (isset($oRow->status))
            $oItem->status      = $oRow->status;   

        return $oItem;
    }

//----------------------------------------------------------------------------------------------------

    public static function getAll($page = 1, $countItemsOnPage = 10)
    {
        $db = self::_getDbTable(self::$_dbTable);
        $select  = $db->select();

        $select->from($db, array('*'));
        $select->setIntegrityCheck(false);

        $select->order('id DESC');
//      echo $select; die;

        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setItemCountPerPage($countItemsOnPage)
                  ->setCurrentPageNumber($page);

        $result = array();
        foreach ($paginator as $page)
        {
            $result[] = (object)$page;
        }

        $ret['array'] = $result;
        $ret['paginator'] = $paginator;

        return $ret;
    }

//----------------------------------------------------------------------------------------------------

    public static function findByIdUser($idUser, $date)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $select = $DbTable->select();
        $select->setIntegrityCheck(false);

        $select->from($DbTable, array('*'));
        

        $select->where('id_user = ?', $idUser);
        $select->Where('date = ? ', $date );
//      echo $select;die;
        $result = $DbTable->fetchAll($select);

        return $result->toArray();
    }

//----------------------------------------------------------------------------------------------------

    public static function save($data)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $result = $DbTable->insert($data);
        return $result;
    }

//----------------------------------------------------------------------------------------------------

    public static function edit($id, $data)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $Update = $DbTable->update($data,  'id = ' . $id);

        return $Update;
    }

//----------------------------------------------------------------------------------------------------

    /**
     * Deletes project from table
     *
     * @param $iId
     */
    public static function delete($iId)
    {
        $oDbTable = self::_getDbTable(self::$_dbTable);
        $where = $oDbTable->getAdapter()->quoteInto('id = ?', $iId, Zend_Db::INT_TYPE);
        $oDbTable->delete($where);
    }

//
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------

}
